package com.crealth.job;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.crealth.crmserver.SessionManager;
import com.crealth.memcached.UserCached;

public class HeartbeatJob implements Job {
	public static Map<Integer, Long> heartbeatMap = 
			new ConcurrentHashMap<Integer, Long>();
	
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		Iterator<Map.Entry<Integer, Long>> entries = heartbeatMap.entrySet().iterator();		  
		while (entries.hasNext()) {		  
		    Map.Entry<Integer, Long> entry = entries.next();
		    long prebeatTm = entry.getValue();
		    long curtm = System.currentTimeMillis() / 1000;
		    int userID = entry.getKey();
		    if ((curtm-prebeatTm) > 5*60) {
		    	UserCached.updateUserOnlineStatus(userID, false);
				SessionManager.removeUserSessionByID(userID);
				entries.remove();
		    }
		    else {
		    	UserCached.updateUserOnlineStatus(userID, true);
		    }
		}
		
	}
	
}
